import { BaseData, BaseMethod } from '@/utils/basc-data'
import { ElMessage, ElMessageBox } from 'element-plus'
import { reactive } from 'vue'
import * as tableConfig from './config.json'
import { UserListParams, enable, userList } from '@/apis/system/account'
import { adminDelete } from '@/apis/company/admin'

export default class SystemAccount extends BaseData implements BaseMethod {
    //弹窗数据
    dialogData: any = reactive({
        visible: false,
        type: 'create',
        isEditAdmin: false,
        config: {}
    })

    searchResult: any = {
        company: '',
        name: '',
        phone: '',
        status: '2' //默认查询手机号
    }
    searchCompany = reactive({
        width: '180px',
        name: '',
        disabled: false
    })
    companyId = ''

    constructor() {
        super(tableConfig)
        if (this.uStore.isSuperAdmin) {
            this.pageData.search.unshift(['公司名称|编号'])
        }
        this.getData()
    }
    onOperation = (arg?: any) => {
        const { type, data } = arg
        if (type === 'edit' || type === 'create') {
            this.dialogData.visible = true
            this.dialogData.type = type
            this.dialogData.config = type === 'edit' ? data : {}
        } else if (type === 'delete') {
            ElMessageBox.confirm(`确定要删除【${data.name}】账号吗？`, '删除', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            })
                .then(() => {
                    //this.isClick = false
                    adminDelete(data.id).then(res => {
                        if (res.data.value === 200) {
                            ElMessage.success('账号删除成功')
                        } else {
                            ElMessage.error(res.data.desc)
                        }
                        this.getData()
                    })
                })
                .catch(() => {
                    //this.isClick = false
                })
        } else if (type === 'switch') {
            console.log('data', data)
            /**
            * 超级管理
              row.id === _userStore.companyId 则不可禁用
              企业管理员 标识为1 且不是 超级管理员 则不可禁用
              row.companyAdminFlag === 1 && !_userStore.isSuperAdmin
            */
            if (data.id === this.uStore.companyId || (data.companyAdminFlag === 1 && !this.uStore.isSuperAdmin)) {
                //
            } else {
                enable(data.id, Number(data.status)).then(res => {
                    console.log(res)
                    if (res.data.value === 30007) {
                        ElMessage.error('该账户已存在企业管理员，不能启用！')
                    } else if (res.data.value === 200) {
                        ElMessage.success(data.status ? '账号启用成功！' : '账号禁用成功')
                    } else {
                        ElMessage.error(res.data.desc)
                    }
                    this.getData()
                })
            }
        }
    }
    /**
     * 监听检索选择的公司id
     * @param id
     */
    onSelectCompanyId = (id: string) => {
        console.log(id)
        this.companyId = id
        this.searchCompany.name = id
    }
    /**
     * 获取数据
     * @param config
     */
    getData() {
        const params: UserListParams = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize,
            companyId: this.uStore.isSuperAdmin ? this.searchResult.companyId : this.uStore.companyId,
            name: this.searchResult.name,
            phone: this.searchResult.phone
        }
        console.log(params, this.uStore.isSuperAdmin, this.uStore.company)
        if (Number(this.searchResult.status) !== 2) {
            params.status = this.searchResult.status
        }
        userList(params).then(res => {
            this.pageData.table.data.length = 0
            if (res.data.value === 200) {
                const arr: any[] = JSON.parse(JSON.stringify(res.data.data.records))
                arr.forEach(item => {
                    item.status = item.status == 1 ? true : false
                })
                this.pageData.table.data.push(...arr)
                this.pageData.pagination.total = res.data.data.total
            }
        })
    }

    /**
     * 监听点击搜索
     */
    onSearch = (res: any) => {
        this.searchResult = res
        this.getData()
    }

    /**
     * 点击重置搜索内容
     */
    onReset = () => {
        this.pageData.pagination.pageNo = 1
        this.searchResult = {
            company: '',
            name: '',
            phone: '',
            status: '2'
        }
        this.companyId = ''
        this.searchCompany.name = ''
        this.getData()
    }

    /**
     * 监听选择每页显示个数
     * @param size
     */
    onPaginationSize = (size: number) => {
        console.log(this)
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = size
        this.getData()
    }

    /**
     * 监听当前分页
     */
    onPaginationChange() {
        this.getData()
    }

    /**
     * 监听对应的状态 启用/禁用
     * @param id
     * @param status
     */
    onUserStatus = (id: string, status: string) => {
        enable(id, Number(status)).then(res => {
            console.log(res)
            if (res.data.value === 30007) {
                ElMessage.error('该账户已存在企业管理员，不能启用！')
            } else if (res.data.value === 200) {
                ElMessage.success(status ? '账号启用成功！' : '账号禁用成功')
            } else {
                ElMessage.error(res.data.desc)
            }
            this.getData()
        })
    }

    /**
     * 监听点击 新增员工 按钮
     */
    isClick = false
    onOpenDialog = (type: string, index = 0) => {
        console.log(this.isClick)
    }

    /**
     * 监听弹窗隐藏
     */
    onDialogClose() {
        this.dialogData.visible = false
        this.isClick = false
        this.getData()
    }
}
